import {simpleGet} from "../../../utils/request";
import list_behavior from "../../../behavior/list_behavior";
import orderService from "../../../service/order";
import {showModel, showToast} from "../../../utils/tool";

const app = getApp()
const global = app.globalData

Page({
    behaviors:[list_behavior],
    data: {
        nav:{
            title:"我的订单"
        },
        options:{},
        imageUrl: global.imageUrl,
    },
    onLoad: function (options={}) {
        this.init(options)
    },

    orderStatusChange(e){
        const {value} = e.detail
        const {list} = this.data
        const where = list.where
        where.order_status = value
        this.setData({
            [`list.where`]:where
        })
        this.setList(1)

    },

    toOrderShow(e){
        const {index} = e.currentTarget.dataset
        const {list} = this.data
        wx.navigateTo({
            url : `/pages/user/order/show?id=${list.items[index].id}`
        })
    },

    payOrder(e){
        const {index} = e.currentTarget.dataset
        const {list, options} = this.data
        const order = list.items[index]
        // 调起支付
        orderService.payment(order).then(r => {
            showToast("付款成功")
            this.init(options)
        })

    },

    /**
     * 确认收货
     */
    receiveHandle(e){
        const {index} = e.currentTarget.dataset
        const {list} = this.data
        const id = list.items[index].id
        showModel("确认收到商品了吗",{
            confirm:() => {
                orderService.receive(id).then(r => {
                    // 将状态改为待评论
                    this.setData({
                        [`list.items[${index}].order_status`]:'reply'
                    })
                })
            }
        })
    },

    replyClick(e){
        const {index} = e.currentTarget.dataset
        const {list} = this.data
        const order = list.items[index]

        wx.navigateTo({
            url:`/pages/user/order/reply_edit?order_id=${order.id}`,
            events:{
                // 添加评论的监听器
                addReplyEvent:() => {
                    this.setData({
                        [`list.items[${index}].order_status`]:'done'
                    })
                }
            }
        })

    },

    async init(options) {
        this.setData({
            options,
            [`list.url`]:'/order/paginate',
            [`list.where`]:{
                order_status:options.order_status||''
            },
        })
        this.setList(1)
    }
});
